{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为__None______."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30,4]`，那么`data[2:100]`的值为_data__[30, 4]___."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为__3______."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为___97_____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(  错  )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`( 错   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`( 对   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`( 对   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:2345663451247\n",
      "Counter({'4': 3, '2': 2, '3': 2, '5': 2, '6': 2, '1': 1, '7': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "text=input('请输入一个字符串:')\n",
    "frequencies=Counter(text)\n",
    "print(frequencies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:1133366488899\n",
      "[('4', 7)]\n"
     ]
    }
   ],
   "source": [
    "text=input('请输入一个字符串:')\n",
    "positions=[(ch,index) for index,ch in enumerate(text) if text.index(ch)==text.rindex(ch)]\n",
    "print(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bcchsha\n",
      "char:a,last site:7\n",
      "char:b,last site:1\n",
      "char:c,last site:3\n",
      "char:h,last site:6\n",
      "char:s,last site:5\n"
     ]
    }
   ],
   "source": [
    "dict1={'a':0,'b':0,'c':0,'f':0,'h':0,'s':0}\n",
    "num=0\n",
    "def function(a,dict1,num):\n",
    "    for i in a:\n",
    "        num+=1\n",
    "        if i in dict1:\n",
    "            dict1[i]=num\n",
    "    return dict1\n",
    "a=str(input())\n",
    "function(a,dict1,num)\n",
    "for x in dict1:\n",
    "    if dict1[x]!=0:\n",
    "        print(\"char:{},last site:{}\".format(x,dict1[x]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:{1,2,3},{1,2},{4,5,6}\n",
      "{1, 2, 3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "last=eval(input('请输入一个列表:'))\n",
    "print(reduce(lambda x,y:x|y,last))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串：45612789\n",
      "no\n"
     ]
    }
   ],
   "source": [
    "str1=input('请输入一个字符串：')\n",
    "str2=str1[::-1]\n",
    "if str1==str2:\n",
    "    print('yes')\n",
    "else:\n",
    "    print('no')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.Python3.x的range()函数返回一个___可迭代对象____"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.表达式“[3]in[1,2,3,4]”的值为__False____"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.列表对象的sort()方法用来对列表元素进行原地排序，该函数返回值为___None_____"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.列表对象的__remove()__方法删除首次出现的指定元素，如果列表中不存在要删除的元素，则抛出异常。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.假设列表对象aList的值为[3,4,5,6,7,9,11,13,15,17]，那么切片aList[3:7]得到的值是___[6,7,9,11]______"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.在Python中，字典和集合都是用一对___大括号___作为界定符，字典的每个元素由两部分组成，即__“键”__和__“值”__,其中__“键”__不允许重复。 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.使用字典对象的__items()__方法可以返回字典的“键：值”对，使用字典对象的__keys()__方法可以返回字典的“键”，使用字典对象的___values()___方法可以返回字典的“值”。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.假设有列表a=['name','age','sex']和b=['Dong',38,'Male'],使用一条语句将这两个列表的内容转换为字典，并且以列表a中的元素为“键”，以列表b中的元素为“值”，这个语句可以写为__c=dict(zip(a,b))_________"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.假设有一个列表a，现要求从列表a中每三个元素取一个，并且将取到的元素组成新的列表b，可以使用语句___b=a[::3]_______"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.使用列表推导式生成包含10个数字5的列表，语句可以写为_[5 for i in range(10)]________。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11.__不可以__（可以，不可以）使用del命令来删除元组中的部分元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12.已知列表data中有若干字符串，要求编写程序，对data中的字符串进行过滤，只输出重复字符不超过一半的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hhijk', 'aacdf']\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "data=['cbc','hhijk','eeey','aacdf']\n",
    "for d in data:\n",
    "    res=Counter(d)\n",
    "    max_len=0\n",
    "    for key in res:\n",
    "        if max_len<res[key]:\n",
    "            max_len=res[key]\n",
    "    n=len(d)            \n",
    "    if max_len>n/2:\n",
    "        data.remove(d)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "13.编写程序，使用筛选法查找并输出小于1000的所有素数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14.为什么应尽量从列表的尾部进行元素的增加与删除操作？\n",
    "   ______提高处理器速度_____"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15.编写程序，生成包含1000个0-100的随机整数，并统计每个元素的出现次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 : 12\n",
      "1 : 13\n",
      "2 : 11\n",
      "3 : 8\n",
      "4 : 15\n",
      "5 : 10\n",
      "6 : 6\n",
      "7 : 8\n",
      "8 : 10\n",
      "9 : 6\n",
      "10 : 9\n",
      "11 : 7\n",
      "12 : 12\n",
      "13 : 6\n",
      "14 : 14\n",
      "15 : 7\n",
      "16 : 8\n",
      "17 : 13\n",
      "18 : 7\n",
      "19 : 13\n",
      "20 : 10\n",
      "21 : 12\n",
      "22 : 9\n",
      "23 : 8\n",
      "24 : 8\n",
      "25 : 11\n",
      "26 : 12\n",
      "27 : 12\n",
      "28 : 10\n",
      "29 : 8\n",
      "30 : 10\n",
      "31 : 12\n",
      "32 : 6\n",
      "33 : 6\n",
      "34 : 9\n",
      "35 : 7\n",
      "36 : 10\n",
      "37 : 14\n",
      "38 : 11\n",
      "39 : 7\n",
      "40 : 20\n",
      "41 : 7\n",
      "42 : 12\n",
      "43 : 4\n",
      "44 : 12\n",
      "45 : 10\n",
      "46 : 8\n",
      "47 : 10\n",
      "48 : 7\n",
      "49 : 9\n",
      "50 : 9\n",
      "51 : 10\n",
      "52 : 10\n",
      "53 : 13\n",
      "54 : 11\n",
      "55 : 8\n",
      "56 : 14\n",
      "57 : 9\n",
      "58 : 9\n",
      "59 : 9\n",
      "60 : 10\n",
      "61 : 5\n",
      "62 : 6\n",
      "63 : 13\n",
      "64 : 7\n",
      "65 : 11\n",
      "66 : 15\n",
      "67 : 13\n",
      "68 : 8\n",
      "69 : 4\n",
      "70 : 16\n",
      "71 : 8\n",
      "72 : 11\n",
      "73 : 16\n",
      "74 : 9\n",
      "75 : 8\n",
      "76 : 12\n",
      "77 : 5\n",
      "78 : 9\n",
      "79 : 9\n",
      "80 : 9\n",
      "81 : 10\n",
      "82 : 10\n",
      "83 : 13\n",
      "84 : 11\n",
      "85 : 16\n",
      "86 : 7\n",
      "87 : 9\n",
      "88 : 10\n",
      "89 : 9\n",
      "90 : 14\n",
      "91 : 16\n",
      "92 : 9\n",
      "93 : 7\n",
      "94 : 12\n",
      "95 : 8\n",
      "96 : 9\n",
      "97 : 14\n",
      "98 : 11\n",
      "99 : 5\n",
      "100 : 8\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x=[random.randint(0,100)for i in range(1000)]\n",
    "d=set(x)\n",
    "for v in d:\n",
    "    print(v,':',x.count(v))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16.编写程序，用户输入一个列表和2个整数作为下标，然后输出列表中介于2个下标闭区间之间的元素组成的子列表。例如用户输入[1,2,3,4,5,6]和2,5，程序输出[3,4,5,6]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表：3,2,5,6,\n",
      "请输入开始位置和末尾位置：2,4\n",
      "(5, 6)\n"
     ]
    }
   ],
   "source": [
    "x=eval(input('请输入一个列表：'))\n",
    "start,end=eval(input('请输入开始位置和末尾位置：'))\n",
    "print(x[start:end+1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17.设计一个字典，并编写程序，用户输入内容作为“键”，然后输出字典中对应的“值”，如果用户输入的“键”不存在，则输出“您输入的不存在！”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个键:3\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "d={1:'a',2:'b',3:'c',4:'d'}\n",
    "v=eval(input('请输入一个键:'))\n",
    "print(d.get(v,'您输入的键不存在'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18.编写程序，生成包含20个随机数的列表，然后将前10个元素升序排列，后10个元素降序排列，并输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[63, 43, 59, 80, 71, 12, 38, 50, 99, 3, 77, 55, 60, 77, 26, 18, 98, 73, 99, 5]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x=[random.randint(0,100)for i in range(20)]\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 12, 38, 43, 50, 59, 63, 71, 80, 99, 99, 98, 77, 77, 73, 60, 55, 26, 18, 5]\n"
     ]
    }
   ],
   "source": [
    "y=x[0:10]\n",
    "y.sort()\n",
    "x[0:10]=y\n",
    "y=x[10:20]\n",
    "y.sort(reverse=True)\n",
    "x[10:20]=y\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
